package com.yangjiayu.Controller;

import com.yangjiayu.bean.ChatEntity;
import com.yangjiayu.service.ChatService;
import com.yangjiayu.service.DocumentService;
import com.yangjiayu.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.document.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Classname RagController
 * @Description TODO
 * @Date 2025/8/14 19:42
 * @Created by YangJiaYu
 */
@RestController
@RequestMapping("rag")
public class RagController {

    @Autowired
    public DocumentService documentService;

    @Resource
    private ChatService chatService;

    @PostMapping("uploadRagDoc")
    public Result uploadRagDoc(@RequestParam("file") MultipartFile file){
        List<Document> documents = documentService.loadText(file.getResource(), file.getOriginalFilename());
        return Result.ok(documents);

    }

    @GetMapping("doSearch")
    public Result doSearch(@RequestParam String question){
        return Result.ok(documentService.doSearch(question));
    }

    @PostMapping("search")
    public void search(@RequestBody ChatEntity chatEntity, HttpServletResponse response){

        List<Document> list = documentService.doSearch(chatEntity.getMessage());
        response.setCharacterEncoding("UTF-8");
        chatService.doChatRagSearch(chatEntity,list);
    }
}














